/* eslint-disable no-case-declarations */
/**
 * @description:统一请求方法 post get jsonp
 * @author：ex_wangshuai
 * @update:2018-05-22
 * @version
 */
import axios from 'axios'
import CONFIG from '../config/config.js'
import URL from '../config/url.js'
import Promise from 'promise'
import originJSONP from 'jsonp'
console.log('应用在' + CONFIG.serviceType + '模式下开发')

/**
 * 返回请求路径
 * @author
 * @param urlTailStr
 */
/** ***************************** TODO ********************************************* */
function getRequestUrl (params) {
  let urlStr = ''
  const serviceType = URL[CONFIG.serviceType]
  const urlData = params || {}
  let name = 'baseUrl'
  if (urlData.hasOwnProperty('urlName')) {
    name = urlData.urlName
  }
  urlStr = serviceType[name] + urlData.url
  console.log(urlStr)
  return urlStr
}
/** ***************************** TODO ********************************************* */
function request (url, params, option) {
  params = params || {}
  option = option || {}
  let config = {}
  option.method = option.method.toUpperCase() // 请求方式全部转换为大写 "POST" "GET" "JSONP"
  const extend = function (dst) {
    // debugger
    for (let i = 1, ii = arguments.length; i < ii; i++) {
      const obj = arguments[i]
      if (obj) {
        const keys = Object.keys(obj)
        for (let j = 0, jj = keys.length; j < jj; j++) {
          const key = keys[j]
          dst[key] = obj[key]
        }
      }
    }
    return dst
  }
  /**
   * @description:post
   * @param {*} params
   * @param {*}
   */
  const post = function (config) {
    return new Promise(function (resolve, reject) {
      extend(config, {
        method: 'post'
      })
      axios(config).then(
        rep => {
          resolve(rep)
        },
        error => {
          reject(error)
        }
      )
    })
  }
  /**
   * @description:get
   * @param {*} params
   * @param {*}
   */
  const get = function (config) {
    console.log(config)
    return new Promise(function (resolve, reject) {
      extend(config, {
        method: 'GET'
      })
      axios(config).then(
        rep => {
          resolve(rep)
        },
        error => {
          reject(error)
        }
      )
    })
  }
  /**
   * @description:jsonp
   * @param {*} params
   * @param {*}
   */
  const getJsonpData = function (url, data, option) {
    url += (url.indexOf('?') < 0 ? '?' : '&') + param(data)
    return new Promise((resolve, reject) => {
      originJSONP(url, option, (err, data) => {
        if (!err) {
          resolve(data)
        } else {
          reject(err)
        }
      })
    })
  }

  function param (data) {
    let url = ''
    for (var k in data) {
      const value = data[k] !== undefined ? data[k] : ''
      url += `&${k}=${encodeURIComponent(value)}`
    }
    return url ? url.substring(1) : ''
  }
  config = extend({
    method: 'JSONP'
  }, option)

  let reqData = {}
  switch (option.method) {
    case 'POST':
      if (option.method === 'POST_JSON') {
        params = JSON.stringify(params)
      }
      reqData = {
        url: url,
        data: params
      }
      config = extend(config, reqData)
      return post(config).then(res => {
        return res.data
      })
    case 'GET':
      reqData = {
        url: url,
        headers: {
          'Content-Type': 'application/json'
        },
        params: params
      }
      config = extend(config, reqData)
      return get(config).then(res => {
        return res.data
      })
    case 'JSONP':
      // console.log(params)
      const data = Object.assign({}, {}, params)
      return getJsonpData(url, data, {
        param: 'callback=jsonpCallback'
      }).then(res => {
        return res
      })
    default:
      console.error('请求方式有误，请检查您的请求方式')
      break
  }
}
/** ***************************** TODO ********************************************* */
function get (url, params, isPro = false) {
  if (isPro) {
    url = getRequestUrl({
      url
    })
  }
  return request(url, params, {
    method: 'get'
  })
}

export {
  getRequestUrl, // 拼接url
  request, // 请求
  get // get请求
}
